﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Data;
using System.Windows.Ria;
using SoftSource.Common;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.ViewModels
{
    public sealed class ProjectMembershipEditorViewModel : IProjectMembershipEditorViewModel
    {
        #region IProjectMemberEditorViewModelConsume Members

        private PagedCollectionView sourceMembers;
        public PagedCollectionView SourceMembers
        {
            get { return this.sourceMembers; }
            private set
            {
                this.sourceMembers = value;
                this.sourceMembers.MoveCurrentToFirst();
                this.PropertyChanged.Notify(() => this.SourceMembers);
            }
        }

        private PagedCollectionView projectMembers;
        public PagedCollectionView ProjectMembers
        {
            get { return this.projectMembers; }
            private set
            {
                this.projectMembers = value;
                this.projectMembers.MoveCurrentToFirst();
                this.PropertyChanged.Notify(() => this.ProjectMembers);
            }
        }

        private Action<object> addToProject;
        public Action<object> AddToProject
        {
            get { return this.addToProject; }
            set
            {
                this.addToProject = obj =>
                    {
                        value.TryInvoke(obj);
                        this.RefreshProjectMembers();
                    };
            }
        }

        private Action<object> removeFromProject;
        public Action<object> RemoveFromProject
        {
            get { return this.removeFromProject; }
            set
            {
                this.removeFromProject = obj =>
                    {
                        value.TryInvoke(obj);
                        this.RefreshProjectMembers();
                    };
            }
        }

        public Func<ProjectMember, Action, UIElement> CreateProjectMemberEditor { get; set; }
        public Action Completed { get; set; }

        public IObservable<Unit> Load { get; set; }

        #endregion

        #region IProjectMemberEditorViewModelService Members

        private EntityCollection<ProjectMember> members;
        public EntityCollection<ProjectMember> Members
        {
            get { return this.members; }
            set
            {                
                this.members = value;
                this.ProjectMembers = new PagedCollectionView(this.members);
            }
        }

        private IEnumerable<UserSummary> memberSource;
        public IEnumerable<UserSummary> MemberSource
        {
            get { return this.memberSource; }
            set
            {
                this.memberSource = value;
                this.RefreshProjectMembers();
            }
        }

        #endregion

        #region Methods
        private void RefreshProjectMembers()
        {
            var filteredUsers = this.MemberSource.Where(user => this.Members.Any(pm => pm.UserID == user.UserID) == false);
            this.SourceMembers = new PagedCollectionView(filteredUsers);
            this.SourceMembers.SortDescriptions.Add(new SortDescription(
                    ObjectReflection.GetPropertyName<UserSummary>(u => u.FullName),
                    ListSortDirection.Ascending));
        }
        #endregion Methods

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion

        ~ProjectMembershipEditorViewModel()
        {
            this.Dispose();
        }
    }
}
